$(document).ready(function() {
	
	setGameOptions();
	setTeamOptions();

	function setGameOptions() {
		fetchGames(function(list) {
			var html = list.reduce(function(memo, item) {
				return memo + ['<option value="' + item.id + '">',item.homeTeam,'vs',item.awayTeam,'</option>'].join('');
			}, '');
			$('.watch-game select').html(html);
		})
	}

	function fetchGames(callback) {
		$.ajax({
			type: 'GET',
			url: 'https://dmpb.com.cn/api/game/list',
			// url: './mock/api:game:list.json',
			success: function(res) {
				callback && callback(res.content);
			}
		});
	}

	function setTeamOptions() {
		fetchTeams(function(list) {
			var html = list.reduce(function(memo, item) {
				return memo + ['<option value="' + item + '">',item,'</option>'].join('');
			}, '');
			$('.info-team select').html(html);
		});
	}

	function fetchTeams(callback) {
		callback && callback(['中国','中国']);
		// $.ajax({
		// 	type: 'GET',
		// 	url: 'https://dmpb.com.cn/api/game/listTeam',
		// 	success: function(res) {
		// 		callback && callback(res.content);
		// 	}
		// });
	}

	/* 注册时间*/

	$('.j-submit').on('click', submitHandle);
	$('.j-add').on('click', addTravel);
	$('.j-del').on('click', delTravel);

	function submitHandle() {
		var data = getSubmitData();
		// $.ajax({
		// 	type: 'post',
		// 	url: '',
		// 	data: {

		// 	},
		// 	success: function(res) {
				
		// 	}
		// });
		window.location = ('../travel_list.html');
	}

	function getSubmitData(){

	}

	function addTravel() {
		$('.content .add-area').before(getTravelInfoHtml());
		$('.j-del').off();
		$('.j-del').on('click', delTravel);
	}

	function delTravel(e) {
		$(e.currentTarget).closest('.list-block').remove();
	}

	function getTravelInfoHtml() {
		return [
			'<div class="list-block my-list-block">',
				'<div class="group-title">景点旅游</div>',
				'<ul>',
					'<li><div class="item-content"><div class="item-media"><i class="icon icon-form-name"></i></div><div class="item-inner"><div class="item-title label">城市</div><div class="item-input"><input type="text" placeholder="请输入出发地"></div></div></div></li>',
					'<li><div class="item-content"><div class="item-media"><i class="icon icon-form-name"></i></div><div class="item-inner"><div class="item-title label">出发日期</div><div class="item-input"><input type="date" placeholder="请输入出发日期"></div></div></div></li>',
					'<li><div class="item-content"><div class="item-media"><i class="icon icon-form-name"></i></div><div class="item-inner"><div class="item-title label">返程日期</div><div class="item-input"><input type="date" placeholder="请输入返程日期"></div></div></div></li>',
					'<li><div class="item-content"><div class="item-media"><i class="icon icon-form-name"></i></div><div class="item-inner"><div class="item-title label">出行方式</div><div class="item-input"><input type="text" placeholder="请输入出行方式"></div></div></div></li>',
					'<li><div class="item-content"><div class="item-media"><i class="icon icon-form-name"></i></div><div class="item-inner"><div class="item-title label">酒店预订</div><div class="item-input"><input type="text" placeholder="请输入酒店预订"></div></div></div></li>',
					'<li class="btn-del"><a href="javascript:;" class="j-del">删除此行程</a></li>',
				'</ul>',
			'</div>'
		].join('');
	}
})
